//
//  PicSelCollectionView.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/9/21.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit

fileprivate let itemMargin : CGFloat = 15.0
fileprivate let picSelCell = "picSelCell"

class PicSelCollectionView: UICollectionView {
    
    // MARK:- 属性
    var imageArr : [UIImage] = [UIImage]() {
        didSet {
            reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let layOut = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (UIScreen.main.bounds.width - 4 * itemMargin) / 3
        layOut.itemSize = CGSize(width: itemWH, height: itemWH)
        layOut.minimumLineSpacing = itemMargin
        layOut.minimumInteritemSpacing = itemMargin
        
        register(UINib.init(nibName: "PicSelCell", bundle: nil), forCellWithReuseIdentifier: picSelCell)
        dataSource = self
        
        //设置collectionView的内边距
        contentInset = UIEdgeInsets(top: itemMargin, left: itemMargin, bottom: 0, right: itemMargin)
    }
}

extension PicSelCollectionView : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArr.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picSelCell, for: indexPath) as! PicSelCell
        cell.image = indexPath.item <= imageArr.count - 1 ? imageArr[indexPath.item] : nil
        return cell
    }
}

